package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/description/
 * @create 2025/9/16 10:27
 * @since JDK17
 */

public class Solution29 {
    public int maxScore(int[] cardPoints, int k) {
        int sum = Arrays.stream(cardPoints).sum(), min = Integer.MAX_VALUE, n = cardPoints.length, cur = 0;
        k = n - k;  //
        if (k == 0) {   // 要取走所有的卡牌
            return sum;
        }
        for (int l = 0, r = 0; r < n; ) {
            while (r < n && r - l < k) {
                cur += cardPoints[r++];
            }
            if (r - l == k) {
                min = Math.min(min, cur);
                cur -= cardPoints[l++];
            }
        }

        return sum - min;
    }

    public int maxScore02(int[] cardPoints, int k) {
        int ans = 0, sum = 0, l = k - 1, r = cardPoints.length - 1;
        for (int i = 0; i < k; i++) {   // 全部拿前面的
            sum += cardPoints[i];
        }
        ans = sum;
        while (l >= 0) {
            sum = sum - cardPoints[l--] + cardPoints[r--];
            ans = Math.max(ans, sum);
        }

        return ans;
    }
}
